package com.newbee.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.hhu.newbee.R;
import com.newbee.adapter.ConferenceFriendsListvewAdapter;
import com.newbee.app.OnRefrushActivity;
import com.newbee.fragement.multchat.MultChatFragement;
import com.newbee.listbean.FriendsInfoBean;
import com.newbee.multchat.MultChatActivity;
import com.newbee.service.MainService;
import com.newbee.service.MainServiceKey;
import com.newbee.service.MainServiceTask;
import com.newbee.service.TaskType;
import com.newbee.smack.SmackClient;
import com.newbee.util.MD5Util;

public class MultChatChooseFriend extends Activity implements
		OnRefrushActivity, OnItemClickListener, OnClickListener
{

	private ListView mListView;

	private View mBack;
	private List<FriendsInfoBean> mDataList;

	private ImageButton mCreate;

	private ConferenceFriendsListvewAdapter mAdapter;
	private List<String> mSelectList = new ArrayList<String>();

	private Builder mBuilder;

	private EditText mConferenceName;
	private SmackClient msc;

	private AlertDialog dialog;

	private TextView mEmptyview;

	public static final String QZ_NAME = "QZ_NAME";

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_create_conference_friendslist);

		MainServiceTask mst = new MainServiceTask(
				TaskType.CONFERENCE_GET_FRIEND, null,
				MultChatChooseFriend.class.getName());

		MainService.addActivityMap(MultChatChooseFriend.class.getName(), this);
		MainService.addTaskQueue(mst);
		msc = SmackClient.getInstance();
		initLayout();
		mEmptyview = (TextView) this.findViewById(R.id.mult_friendlist_empty);
		initDialog();
	}

	private void initDialog()
	{
		this.mBuilder = new AlertDialog.Builder(this);
		this.mBuilder.setTitle("创建房间");
		this.mBuilder.setIcon(R.drawable.ic_launcher_mini);
		View view = LayoutInflater.from(this).inflate(
				R.layout.dialog_conference, null);
		mConferenceName = (EditText) view.findViewById(R.id.conference_name);
		this.mBuilder.setView(view);
		mBuilder.setPositiveButton("创建",
				new android.content.DialogInterface.OnClickListener()
				{
					@Override
					public void onClick(DialogInterface arg0, int arg1)
					{

						if (TextUtils.isEmpty(mConferenceName.getText()
								.toString()))
						{
							Toast.makeText(MultChatChooseFriend.this,
									"协同录音的名称不能为空！", Toast.LENGTH_LONG).show();
							return;
						}

						Map<String, Object> paras = new HashMap<String, Object>();
						paras.put(MainServiceKey.INVITE_FRIEND_LIST,
								mSelectList);
						paras.put(MainServiceKey.MULT_CHAT_ROOM_NAME,
								mConferenceName.getText().toString());
						MainServiceTask mst = new MainServiceTask(
								TaskType.CREATE_MULT_CHAT, paras,
								MultChatChooseFriend.class.getName());
						MainService.addActivityMap(
								MultChatChooseFriend.class.getName(),
								MultChatChooseFriend.this);
						MainService.addTaskQueue(mst);
					}
				});

		mBuilder.setNegativeButton("取消",
				new android.content.DialogInterface.OnClickListener()
				{

					@Override
					public void onClick(DialogInterface arg0, int arg1)
					{

					}
				});
		dialog = this.mBuilder.create();
	}

	private void initLayout()
	{
		mListView = (ListView) this
				.findViewById(R.id.conference_friend_listview);
		mListView.setOnItemClickListener(this);
		mBack = (View) this.findViewById(R.id.conference_friend_choose_back);
		mBack.setOnClickListener(this);
		mCreate = (ImageButton) this.findViewById(R.id.conference_create);
		mCreate.setOnClickListener(this);
	}

	@Override
	public void init()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public RtInfo refresh(int taskID, Object... paras)
	{
		switch (taskID)
		{
		case TaskType.CONFERENCE_GET_FRIEND:
		{

			this.mDataList = filter((List<FriendsInfoBean>) paras[0]);

			if (this.mDataList.size() == 0)
			{
				this.mListView.setEmptyView(mEmptyview);
			}
			else if (mAdapter == null)
			{

				mAdapter = new ConferenceFriendsListvewAdapter(mDataList, this);
				this.mListView.setAdapter(mAdapter);

			}
			else
			{
				mAdapter.setList(mDataList);
				mAdapter.notifyDataSetChanged();
			}

			break;
		}
		case TaskType.CREATE_MULT_CHAT:
		{

			boolean j = (Boolean) paras[0];
			ArrayList<String> users = (ArrayList<String>) paras[2];// 带有后缀的用户名列表
			if (j)
			{

				Intent intent = new Intent(this, MultChatActivity.class);

				Bundle bundle = new Bundle();
				bundle.putCharSequence(MultChatFragement.CHAT_ROOM_MAME,
						MD5Util.multChatRoom(paras[1].toString()));// 纯md5
				bundle.putStringArrayList(MultChatFragement.USERS_LIST, users);

				bundle.putString(QZ_NAME, this.mConferenceName.getText()
						.toString());
				System.out.println("群组create----------------->" + users);
				intent.putExtras(bundle);
				this.startActivity(intent);
			}
			else
			{
				Toast.makeText(this, "创建协同录音失败！", Toast.LENGTH_LONG).show();
			}

			break;
		}

		default:
			break;
		}
		return null;

	}

	private List<FriendsInfoBean> filter(List<FriendsInfoBean> list)
	{

		List<FriendsInfoBean> resultList = new ArrayList<FriendsInfoBean>();

		int size = list.size();
		FriendsInfoBean bean = null;
		for (int i = 0; i < size; i++)
		{
			bean = list.get(i);

			if (bean.isState())
			{
				resultList.add(bean);
			}
		}
		return resultList;

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3)
	{

		CheckBox checkBox = (CheckBox) v.findViewById(R.id.conference_checkbox);

		System.out.println("click---------------------->" + position);
		FriendsInfoBean fib = mDataList.get(position);

		if (checkBox.isChecked())
		{
			checkBox.setChecked(false);
			mSelectList.remove(fib.getEmail());
		}
		else
		{
			checkBox.setChecked(true);
			mSelectList.add(fib.getEmail());
		}

	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.conference_friend_choose_back:
		{
			this.finish();
			break;
		}
		case R.id.conference_create:
		{
			if (mSelectList.size() == 0)
			{
				Toast.makeText(MultChatChooseFriend.this, "协同录音的人不能为空！",
						Toast.LENGTH_LONG).show();
				return;
			}
			dialog.show();
			break;
		}

		default:
			break;
		}

	}

	@Override
	protected void onDestroy()
	{

		MainService.removeActivity(MultChatChooseFriend.class.getName());
		super.onDestroy();
	}

}
